package com.sright.bcard.enterprise.web.controller;

import com.aliyuncs.exceptions.ClientException;
import com.sright.bcard.common.service.share.dto.content.AdminDTO;
import com.sright.bcard.content.share.service.UserService;
import com.sright.bcard.enterprise.web.context.UserContext;
import com.sright.bcard.common.service.share.mvcresolver.EnableLoginAuth;
import com.sright.bcard.common.service.share.dto.user.RegisterDTO;
import com.sright.bcard.user.share.request.LoginParam;
import com.sright.bcard.user.share.request.RegisterParam;
import com.sright.bcard.user.share.service.AuthService;
import com.sright.common.response.SrightResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;

/**
 * @author peter
 * @description
 * @date 2020/5/22
 */
@RestController
@RequestMapping("/auth")
public class LoginController extends BaseController{

     @Autowired
     private AuthService authService;

     @Autowired
     private UserService userService;

     /**
      * 管理员登录
      * @param body
      * @return
      */
     @PostMapping("/login.json")
     public SrightResponse login(@Validated @RequestBody LoginParam body) {
         return authService.login(body.getUsername(), body.getPassword());
     }

     /**
      * 登录注销
      * @return
      */
     @EnableLoginAuth
     @GetMapping("/logout.json")
     public SrightResponse logout() {
         return SrightResponse.defaultSuccess();
     }

    /**
     * 开通企业账号
     * @param body
     * @return
     */
    @PostMapping("/register.json")
    public SrightResponse register(@Validated @RequestBody RegisterParam body) throws ParseException {
        RegisterDTO dto = new RegisterDTO();
        BeanUtils.copyProperties(body, dto);
        return authService.register(dto);
    }

    /**
     * 获取联系人手机号
     * @param mobile
     * @return
     */
    @GetMapping("/contactsMobile.json")
    @ResponseBody
    @EnableLoginAuth
    public SrightResponse contactsMobile(@RequestParam(required = false) String mobile) {
        return userService.contactsMobile(mobile);
    }

    /**
     * 发送短信
     * @param adminDTO
     * @return
     */
    @PostMapping("/sendMsg.json")
    @ResponseBody
    public SrightResponse sendMsg(@RequestBody AdminDTO adminDTO) throws ClientException {
        buildLogUserContext(adminDTO, userContext);
        return userService.smsSend(adminDTO);
    }

    /**
     * 重置密码
     * @param adminDTO
     * @return
     */
    @PostMapping("/resetPassword.json")
    @ResponseBody
    public SrightResponse resetPassword(@RequestBody AdminDTO adminDTO) {
        buildLogUserContext(adminDTO, userContext);
        return userService.resetPassword(adminDTO);
    }

    /**
     * 校验手机号
     * @param adminDTO
     * @return
     */
    @PostMapping("/checkCode.json")
    @ResponseBody
    public SrightResponse checkCode(@RequestBody AdminDTO adminDTO) {
        buildLogUserContext(adminDTO, userContext);
        return userService.checkCode(adminDTO);
    }

    private AdminDTO buildLogUserContext(AdminDTO AdminDTO, UserContext userContext) {
        AdminDTO.setUserId(userContext.getUserId());
        AdminDTO.setToken(userContext.getToken());
        AdminDTO.setEnterpriseId(userContext.getEnterpriseId());
        return AdminDTO;
    }

}
